﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Xml;
using DotUpdater.Core;
using DotUpdater.Core.VersionGeters;
using DotUpdater.Core.Versions;
using DotUpdater.Core.CurrentFilesGeters;
using DotUpdater.Core.LocalFilesAccessors;
namespace DotUpdater.AppStart
{
    public partial class FrmWating : Form
    {
        public FrmWating()
        {
            InitializeComponent();
            Control.CheckForIllegalCrossThreadCalls = false;
        }

        private void FrmWating_Load(object sender, EventArgs e)
        {

        }

        private void FrmWating_Shown(object sender, EventArgs e)
        {
            try
            {
                System.Threading.Thread th = new System.Threading.Thread(new System.Threading.ThreadStart(m_processUpdate));
                th.Start();
            }
            catch (System.Exception)
            {
                if (MessageBox.Show("更新程序出现了异常，系统将直接启动主程序", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1) == DialogResult.Yes)
                {
                    Program.OpenMainProgram();
                }
            }       
        }

        private void m_processUpdate()
        {
            Updater updater = new Updater(new LocalVersionGeter(Configer.LocalVersion, Configer.LocalFilesPath), new FilesConfigVersionGeter(Configer.FileListURL), new HttpCurrentFilesGeter(Configer.CurrentFilesHttpAddress), new LocalFilesAccessor(System.IO.Path.Combine(System.Environment.CurrentDirectory, Configer.LocalFilesPath)));


            if (updater.IsNeedUpdate())
            {

                FrmMain frm = new FrmMain(updater);
                frm.StartPosition = FormStartPosition.CenterScreen;
                this.Hide();
                frm.ShowDialog();
            }
            else
            {
                Program.OpenMainProgram();
            }
        }
    }
}
